//
//  HTRootViewController.m
//  HTDemo
//
//  Created by meet on 14-3-14.
//  Copyright (c) 2014年 HT. All rights reserved.
//

#import "HTRootViewController.h"
#import "HTTableViewObject.h"
#import "HTPieChartViewController.h"
#import "HTPopViewViewController.h"
#import "HTOtherInfoViewController.h"
#import "HTThankViewController.h"
static CGFloat const kRowHeight  =  80;

@interface HTRootViewController ()<UITableViewDataSource,UITableViewDelegate>
@property (nonatomic, strong) NSArray *demoItems;
@end

@implementation HTRootViewController
{
    UITableView *_tableView;
}

#pragma mark - 生命周期
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.navigationItem.title = @"Demo展示";
    [self drawTableView];

}

#pragma mark - 绘制视图
- (void)drawTableView
{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, Screen_Width, kRowHeight*self.demoItems.count) style:UITableViewStylePlain];
    HTLogRect(_tableView.frame);
    _tableView.dataSource = self;
    _tableView.delegate = self;
    _tableView.backgroundColor = [UIColor clearColor];
    _tableView.scrollEnabled = NO;
    [self.view addSubview:_tableView];
}

#pragma mark - getter
- (NSArray *)demoItems
{
    if(_demoItems == nil){
        
        _demoItems = @[[HTTableViewObject objectWithImageNamed:@"pieChart" _title:@"弹出菜单"],
                       [HTTableViewObject objectWithImageNamed:@"popView" _title:@"饼状图"],
                       [HTTableViewObject objectWithImageNamed:@"info" _title:@"其他信息"],
                       [HTTableViewObject objectWithImageNamed:@"thanks" _title:@"想说的话"]];
    }
    
    return _demoItems;
}

#pragma mark - tableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _demoItems.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"HTRootViewControllerCellIdentifier";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    
    if(cell == nil){
        
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellIdentifier];
        cell.selectionStyle = UITableViewCellSelectionStyleBlue;
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    HTTableViewObject *object = self.demoItems[indexPath.row];
    cell.imageView.image = [UIImage imageNamed:object.imageName];
    cell.textLabel.text = object.title;
    
    return cell;
}

#pragma mark - tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return kRowHeight;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(indexPath.row == 0){
        
        HTPopViewViewController *popVC = [[HTPopViewViewController alloc] init];
        [self.navigationController pushViewController:popVC animated:YES];
    }
    
    if(indexPath.row == 1){
        
        HTPieChartViewController *pieChartVC = [[HTPieChartViewController alloc] init];
        [self.navigationController pushViewController:pieChartVC animated:YES];
    }
    
    if(indexPath.row == 2){
        
        HTOtherInfoViewController *infoVC = [[HTOtherInfoViewController alloc] init];
        [self.navigationController pushViewController:infoVC animated:YES];
    }
    if(indexPath.row == 3){
        
        HTThankViewController *thanksVC = [[HTThankViewController alloc] init];
        [self.navigationController pushViewController:thanksVC animated:YES];
    }
    
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
}

@end
